//地图
define(function(require,exports,module){
    require("easyui");
    require("easyui-css");
    require("easyui-icon-css");
    var $ = require("$");
/*
* 地图生成类
* @param point： 页面中用于存储标注点坐标的input ID
* @param location：页面中用于存储标注点地理位置的input ID
* */
    function Map(city,point,location){
        this.pointId = point;
        this.locationId = location;
        this.city = city;
        this.map = null;
        this.gc = new BMap.Geocoder();
        this.marker = null;
        this.openInfo = false;
        this.infoWindow;
    }
    module.exports = Map;//对外提供的接口

    /* 地图展示*/
    Map.prototype.showMap = function(mapId,editable,name,address,phone){
        var that = this;
        //that.infoWindow = new BMap.InfoWindow("<h6 style='color:#ed2d2d'>温馨提示：</h6>您可以在地图中点击右键添加标注。");
        var infoWindowHtml = '',showInfoWindow = null;
        if(name){
            infoWindowHtml = "<h5 style='color:#F60;font-size: 16px'>"+name+"</h5>地址："+address+"<br />电话："+phone+"</p>";
            showInfoWindow = new BMap.InfoWindow(infoWindowHtml);
        }
        that.map = new BMap.Map(mapId);
        that.map.enableScrollWheelZoom();
        if($.isNumeric(that.city.substr(0,1))){
            var point = that.city.split(","),
                p = new BMap.Point(point[0],point[1]);
            that.map.centerAndZoom(p,13);
            that.marker = new BMap.Marker(p);  // 创建标注
            that.map.addOverlay(that.marker);
            if(!editable){
                that.editMarker(that.marker,showInfoWindow);
            }
        }else{
            //地址解析
            that.gc.getPoint(that.city, function(point){
                if (point) {
                    that.marker = new BMap.Marker(point);
                    that.map.centerAndZoom(point,15);
                    that.map.addOverlay(that.marker);
                    if(!editable){
                        that.editMarker(that.marker,showInfoWindow);
                    }
                }
            });
        }
    }

    Map.prototype.createMap = function(mapId,editable){
        var that = this;
        //新建信息面板
        //创建地图
        that.showMap(mapId,editable);
        that.map.addEventListener('rightclick', function(e){
            that.map.clearOverlays();
            that.marker = new BMap.Marker(e.point)
            that.map.addOverlay(that.marker);
            that.addMarker(e,that.gc);
        });
    }
    Map.prototype.editMarker = function(marker,info){
        var that = this;
        marker.openInfoWindow(info);
        that.openInfo = true;
        //点击显示/关闭提示
        marker.addEventListener("click",function(){
            if(that.openInfo){
                this.closeInfoWindow(info);
                that.openInfo = false;
            }else{
                this.openInfoWindow(info);
                that.openInfo = true;
            }
        });
    }
    Map.prototype.addMarker = function(e,gc){
        var that = this;
        var w = $('body').width(),h = $('body').height();
        var moveLeft = w-430;
        var moveTop =  h-350;
        $("#confirm").dialog({
            closed:true,
            modal:true,
            width:300,
            height:150,
            left:moveLeft,
            top:moveTop,
            title:'提示',
            buttons:[{
                text:"添加",
                handler:function(){
                    var pt = e.point;
                    $("#confirm").dialog("close");
                    $(window.parent.document).find('#'+that.pointId).val(pt.lng+","+pt.lat);
                    //地址解析
                    gc.getLocation(pt, function(rs){
                        var addComp = rs.addressComponents,
                            loc = addComp.province==addComp.city ? addComp.city+" "+addComp.district : addComp.province+" "+addComp.city+" "+addComp.district;
                        $(window.parent.document).find('#'+that.locationId).val(loc);
                    });
                }
            },
            {
                text:"不添加",
                handler:function(){
                    $("#confirm").dialog("close");
                }
            }]
        });
        $("#confirm").dialog('open')
    }
});
